package com.rtx.wms.std.handler.asn;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.asn.bean.ApiReceiptdetailfinish;
import com.rtx.wms.std.asn.bean.ApiReceiptfinish;
import com.rtx.wms.std.asn.bean.Receipt;
import com.rtx.wms.std.asn.bean.Receiptdetail;
import com.rtx.wms.std.asn.service.ApiReceiptfinishService;
import com.rtx.wms.std.asn.service.ReceiptService;
import com.rtx.wms.std.common.bean.WmsConstant;

import cn.hutool.core.util.IdUtil;

@Component
public class ReceiptFinishHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ReceiptService receiptService;

    @Autowired
    private ApiReceiptfinishService apiReceiptfinishService;

    /**
     * 处理完成的入库单
     * @param whseid
     */
    @DataSourceAnnotation
    public void handlerReceiptFinish(String whseid){
        //获取待回传的入库单
        List<Receipt> receiptList = selectReceipt(whseid);
        for(Receipt o : receiptList){
            //写入回传中间表
            addApiReceiptfinish("ris_edi",o);
            //更新状态
            updateReceipt(whseid,o);
        }
    }

    /**
     * 写入回传中间表
     * @param whseid
     * @param o
     */
    private void addApiReceiptfinish(String whseid, Receipt o) {
        try {
            ApiReceiptfinish apiReceiptfinish = convertApiReceiptfinish(o);
            apiReceiptfinishService.addApiReceiptfinish(whseid, apiReceiptfinish);
            o.setEdiflag(WmsConstant.SUCCESS_FLAG);
        } catch (Exception e) {
            logger.error("仓库：{}，入库单号：{}写入回传中间表异常",whseid,o.getReceiptkey());
            o.setEdiflag(WmsConstant.FAIL_FLAG);
            o.setEdimsg(e.getMessage());
        }
    }

    /**
     * 转化类型
     * @param o
     * @return
     */
    private ApiReceiptfinish convertApiReceiptfinish(Receipt o) {
        //获取唯一编号
        String batchid = IdUtil.getSnowflakeNextIdStr();
        ApiReceiptfinish apiReceiptfinish = new ApiReceiptfinish();
        BeanUtils.copyProperties(o,apiReceiptfinish,"details");
        apiReceiptfinish.setRtxqueue(0);
        apiReceiptfinish.setEdiflag("P");
        List<ApiReceiptdetailfinish> details = new ArrayList<>();
        for(Receiptdetail od : o.getDetails()){
            ApiReceiptdetailfinish detail = new ApiReceiptdetailfinish();
            BeanUtils.copyProperties(od,detail);
            detail.setQtyexpected(od.getQtyexpected()+"");
            detail.setQtyreceived(od.getQtyreceived()+"");
            detail.setBatchid(batchid);
            detail.setLottable01(od.getLottable01());
            detail.setLottable02(od.getLottable02());
            detail.setLottable03(od.getLottable03());
            detail.setLottable04(od.getLottable04());
            detail.setExternlineno(od.getExternlineno());
            details.add(detail);
        }
        apiReceiptfinish.setBatchid(batchid);
        apiReceiptfinish.setDetails(details);
        return apiReceiptfinish;
    }

    /**
     * 更新入库单
     * @param whseid
     * @param o
     */
    private void updateReceipt(String whseid, Receipt r) {
        receiptService.updateDetailStatusByWrapper(whseid,r);
    }

    /**
     * 查询入库单
     * @param whseid
     * @return
     */
    public List<Receipt> selectReceipt(String whseid){
        LambdaQueryWrapper<Receipt> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
        .eq(Receipt::getStatus,"11")
//        .eq(Receipt::getEdiflag,WmsConstant.DEFAULT_FLAG)
        .exists("select 1 from po where pokey = receipt.pokey and toexamine = '0' ")
        .exists("select 1 from receiptdetail where receiptkey = receipt.receiptkey and rtxeditflag_erp in ('P', 'PF') and rtxqueue_erp < '3' ");

//        .in(Receipt::getrtx, WmsConstant.DEFAULT_FLAG, WmsConstant.FAIL_FLAG);
//        queryWrapper
//        .in(Receipt::getReceiptkey,"ASN2403120000011");
        return receiptService.selectReceiptByWrapper(whseid,queryWrapper);
    }
}
